package coderead.tomcat;
/**
 * @Copyright 源码阅读网 http://coderead.cn
 */

import org.apache.tomcat.util.http.fileupload.IOUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author 鲁班大叔
 * @date 2021
 */
public class HttpClient {
    private static String urlText = "http://127.0.0.1:8080/ping";

    public static void main(String[] args) throws IOException {
       /*pings(3);
       System.in.read();*/
        ping();
    }

    public static void pings(int count) throws IOException {
        for (int i = 0; i < count; i++) {
            new Thread(() -> {
                try {
                    ping();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }

    private static void ping() throws IOException {
        URL url = new URL(urlText);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.connect();
        OutputStream outputStream = connection.getOutputStream();
        outputStream.write("ping".getBytes());
        InputStream inputStream = connection.getInputStream();
        byte[] b = new byte[1024];
        int size = inputStream.read(b);
        System.out.println(new String(b,0,size));
    }


}
